function rowBox(obj){
  var title=obj.title||'';
  var role=obj.role||''
  var is_active=obj.is_active||false;
  var is_footer=obj.is_footer||true;
  var count1={
    key:obj.count1.key||0,
    text:obj.count1.text||''
  }
  var count2={
    key:obj.count2.key||0,
    text:obj.count2.text||''
  }
  var count3={
    key:obj.count3.key||0,
    text:obj.count3.text||'',
  }
  return `
  <div class="row-box ${is_active?'active':''}" role="${role}">
    <div class="row row-header">
      <div class="col-xs-8 row-title"><strong>${title}</strong></div>
      <div class="col-xs-4 row">
        <div class="col-xs-4 p0 tac row-count"><p><strong>${count1.key}</strong></p><p><sub>${count1.text}</sub></p></div>
        <div class="col-xs-4 p0 tac row-count"><p><strong>${count2.key}</strong></p><p><sub>${count2.text}</sub></p></div>
        <div class="col-xs-4 p0 tac row-count"><p><strong>${count3.key}</strong></p><p><sub>${count3.text}</sub></p></div>      
      </div>
    </div><!-- row-header end  -->
    <div class="row row-body">
      <div class="col-xs-12 tac refresh p0 ">
        <i class="glyphicon glyphicon-refresh"></i>
     </div>
    </div>
    <!-- row-body end -->
    ${is_footer?`
    <div class="row row-footer">
      <div class="col-xs-12 tac" role="toggle">
        <i class="glyphicon glyphicon-chevron-down"></i>
      </div>
    </div>
    `:''}
  </div>
  `
}
function rowBoxRender(obj){
  var html=rowBox(obj);
  return $(html)
}
function getUser(cb) {
  $.post('/user/query',{query:{_id:__.data('user')._id}},function(resData) {
    if(resData.code==200){
      console.log(resData.data[0])
      __.data('user',resData.data[0]);
      if(__.data('user').statu=="baned"){
        alert('您已被封禁，请联系管理员')
        location.href="/lnr.html";
      }
      if(!!cb){
        cb();      
      }
    }
  })
}
// element resize
$(window).on('inited',function(){
  $('[data-toggle="tooltip"]').tooltip()
  $('#myTabs a').click(function (e) {
    e.preventDefault()
    var target=$($(this).attr('href'));
    $(e.target).parents('li').addClass('active').siblings().removeClass('active')
    target.addClass('active').siblings().removeClass('active')
  })
  function setMCTop(){
    var navH = $('#nav-top').height();
    $('#main-container').css('top',navH+'px')
  }
  function setCBH(){
    var navH = $('#nav-top').height();
    var WH=$(window).height();
    $('.chat-box').css('height',WH-navH);
    $('#row-box-container').css('height',WH-navH);        
    $('.tab-pane').css('height',WH-navH);  
    $('#main-container').css('height',WH-navH);         
  }
  setMCTop()
  setCBH()
  $('#user_center [role="tablist"] a').click(function (e) {
    e.preventDefault()
    $(this).tab('show')
  })
  
  $(window).on('resize',function(){
    setMCTop()
    setCBH()
  })
  $('#chat-box-input').quickEdit({
    media:true,
    emoji:true,
    fontSize:true,
  })
  // $('.row-box.active>.row-body').mCustomScrollbar();
  $('#row-box-container').on('click','.row-box .row-header .row-title',function(){
   var _this=$(this).parents('.row-box')

   $(_this).addClass('active').siblings().removeClass('active')
  })
  $('#row-box-container').on('click','.row-box [role=toggle]',function(e){
    $(this).parents('.row-box').first().toggleClass('active')
  })
})
// socket IO 即时聊天
$(window).on('inited',function(){
  //socketIO----------------------------------------------------------------
  // ----------设置昵称-------------
  if(!!__.data('user')){
    var userName = __.data('user').name
  }else{
    var userName = "游客" + Math.ceil(Math.random() * 100000);
  }
  // console.log(userName)

  // ---------创建连接-----------
  var socket = io();
  // 加入房间
  socket.on('connect', function () {
    socket.emit('join', { "userName": userName, "room": 'pic' });
  });
  // 监听消息
  socket.on('msg', function (uName, msg) {
    if (userName != uName) {
      $.get('/user/query',{
        query:{
          name:uName
        }},function (resData) {
          console.log(resData)
          var message = `
          <div class="message">
            <div class="msg-head" style="background-image: url('${resData.data[0].head_img}')">
            </div> 
            <div class="msg-box">
              <sup>${uName}</sup>
              <div class="msg">${msg}</div>
            </div>
          </div>`;
         $('#chat-box-screen').append(message);
        })
    } else {
      var message = `
            <div class="message message-me">
              <div class="msg-box">
                <sup>${uName}</sup>
                <div class="msg">${msg}</div>
              </div>
              <div class="msg-head" style="background-image: url('${__.data("user").head_img}')">
              </div> 
            </div>`;
    }
    $('#chat-box-screen').append(message);
    // 滚动条保持最下方
    $('#chat-box-screen').scrollTop($('#chat-box-screen')[0].scrollHeight);
  });
  // 切换房间
  $('body').on('click','[role=topic]>.row-header .row-title',function () {
    var name=$(this).text().trim()
    socket.emit('leave');
    $('#chat-box-screen').html('');        
    $('#chat-box-title').text(name);    
    socket.emit('join', { "userName": userName, "room": name });
  })
  $('body').on('click','[role=topic]>.row-footer [role="toggle"]',function () {
    socket.emit('leave');
    $('#chat-box-screen').html('');  
    $('#chat-box-title').text('广场');    
    socket.emit('join', { "userName": userName, "room": 'pic' });
  })
  // 监听系统消息
  socket.on('sys', function (sysMsg, users) {
    var message = '<div class="sysMsg">' + sysMsg + '</div>';
    $('#chat-box-screen').append(message);
    $('#count').text(users.length);
    $('#users').text(users);
  });
  // 发送消息  
  $('#chat-box-input .editor').on('keydown',function (e) {
    if(e.ctrlKey&&e.keyCode==13){
      $('#chat-box-submit').trigger('click')
    }
  })
  $('#chat-box-submit').on('click', function () {
    var msg = $('#chat-box-input .editor').html()
    $('#chat-box-input .editor').html('');
    socket.send(msg);
  })


  // 退出房间
  $('#joinOrLeave').click(function () {
    if ($(this).text() === '退出房间') {
      $(this).text('进入房间');
      socket.emit('leave');
      var msg = '你已经退出了房间,重新发言请点击"进入房间"';
      $('#msglog').append('<div class="sysMsg">' + msg + '</div>');
    } else {
      $(this).text('退出房间');
      socket.emit('join', userName);
    }

  });
})
// 用户注销
$(window).on('inited',function(){
  if(!!__.data('user')){
    getUser(function(){
      var userData=__.data('user');
      $('#user_dropdown').html(`
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span id="user">${userData.name}</span> <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li class=""><a href="/user/logout" role="logout">注销</a></li>
      </ul>
      `).find('[role=logout]').on('click',function(e){
        __.del('user')
      })
    })
    
  }else{
    location.href='/lnr.html'
  }
})
// 快速搜索
$(window).on('inited',function(){
  $('#qs').on('submit',function(e){
    e.preventDefault()
    var formData=$(e.target).serializeObject();
    var reg=new RegExp(formData.key_word,'i');
    // var reqData={
    //   query:{statu:{"$ne":"baned"},name:{$regex:reg}}
    // }
    $('#tab-pane-other').addClass('active').siblings().removeClass('active')
    $('#myTabs li').removeClass('active')
    if(formData.search_type=='topic'){
      let reqData={
        query:{statu:{"$ne":"baned"},name:formData.key_word}
      }
      $.post(`/topic/fuzzy`,reqData,function(resData){
        // console.log(resData)
        var tpoHtml=`
        <div class="row-box active">
          <div class="row row-header">
            <div class="col-xs-8 row-title"><strong>没找到？创建一个！</strong></div>
            <div class="col-xs-4 row">   
            </div>
          </div><!-- row-header end  -->
          <div class="row row-body">
            <div class="col-xs-12 refresh">
              <form id="tpo_form">
                <div class="form-group">
                  <label for="topic_name">话题名称：</label>
                  <input type="text" class="form-control" id="topic_name" name="name" placeholder="" value="${formData.key_word}">
                </div>
                <button type="submit" class="btn btn-default">创建</button>
              </form>
            </div>
          </div>
          <!-- row-body end -->
          <div class="row row-footer">
            <div class="col-xs-12 tac" role="toggle">
              <i class="glyphicon glyphicon-chevron-down"></i>
            </div>
          </div>
        </div>
        `;
        if(resData.data.length==0&&formData.search_type=="topic"){
          $('#tab-pane-other').html(tpoHtml)
        }else{
          var html=''
          var t=false;
          // console.log(resData)
          resData.data.forEach(item => {
            t=item.name==formData.key_word?t:true;
            html+=`
            <div class="row-box" role="topic" data-id="${item._id}" data-admin="${item.admin_id||''}">
              <div class="row row-header">
                <div class="col-xs-8 row-title"><strong>${item.name}</strong></div>
                <div class="col-xs-4 row">
                  <div class="col-xs-4 p0 tac row-count"><p>${!!!item.admin_name||item.admin_name==""?`<strong data-id="${item._id}" data-name="${item.name}" role="be_admin">成为版主！</strong>`:`<strong>${item.admin_name||''}</strong>`}</p><p><sub>管理员</sub></p></div>
                  <div class="col-xs-4 p0 tac row-count"><p><strong>${item.like_count||0}</strong></p><p><sub>关注</sub></p></div>
                  <div class="col-xs-4 p0 tac row-count"><p><strong>${item.p0000_count||0}</strong></p><p><sub>主题</sub></p></div>                  
                </div>
              </div><!-- row-header end  -->
              <div class="row row-body">
                <div class="col-xs-12 tac refresh p0 ">
                  <i class="glyphicon glyphicon-refresh"></i>
                </div>
              </div>
              <button class="btn btn-info add-btn" role="likeTip" data-name="${item.name}" data-id="${item._id}"><i class="glyphicon glyphicon-star"></i></button>            
              <button class="btn btn-info add-btn" role="addTip" data-name="${item.name}" data-id="${item._id}"><i class="glyphicon glyphicon-send"></i></button>            
              <!-- row-body end -->
              <div class="row row-footer">
                <div class="col-xs-12 tac" role="toggle">
                  <i class="glyphicon glyphicon-chevron-down"></i>
                </div>
              </div>
            </div>
            `
          });
          html=t?tpoHtml+html:html;
          $('#tab-pane-other').html(html)
        }
        $('#tab-pane-other').find('#tpo_form').on('submit',function(e){
          var tarForm=$(e.target)
          e.preventDefault();
          var tpoFormData=$(e.target).serializeObject();
          // console.log(tpoFormData);
          var reqData={
            query:{
              name:tpoFormData.name,
              creater_name:__.data('user').name,
              creater_id:__.data('user').id
            }
          }
          $.post('/topic/add',reqData,function(resData){
            // console.log(resData)
            if(resData.code==200){
              tarForm.before(`
              <div class="alert alert-success alert-dismissible fade in" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
                <strong>创建成功</strong>
              </div>
              `)
            }else{
              tarForm.before(`
              <div class="alert alert-danger alert-dismissible fade in" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
                <strong>创建失败，该话题已存在，如果未显示该话题，则该话题被封禁</strong>
              </div>
              `)
            }
          })
  
        })
      })
    }
    if(formData.search_type=='note'){
      let reqData={
        query:{
          name:formData.key_word,
          statu:{"$ne":"baned"},
          tar_id:'0000',
        }
      }
      var banedTopic,banedUser,resData={}
      $.when(
        $.post('/topic/query',{
          query:{$or:[{statu:'baned'}]}
        },function (resData) {;
          banedTopic=resData
        }),
        $.post('/user/query',{
          query:{$or:[{statu:'baned'}]}
        },function (resData) {;
          banedUser=resData
        }),
        $.post('/note/fuzzy',reqData,function(res) {
          resData=res;
        })
      ).done(function(){
        if(resData.code==200&&banedTopic.code==200&&banedUser.code==200){
          var banedTopicIdArr=[];
          var banedUserIdArr=[];
          banedTopic.data.forEach(item => {
            banedTopicIdArr.push(item._id)
          });
          banedUser.data.forEach(item => {
            banedUserIdArr.push(item._id)
          });
          var html='';
          resData.data.forEach(item => {
            if(banedTopicIdArr.indexOf(item.topic_id)==-1&&banedUserIdArr.indexOf(item.author_id)==-1){
              html+=`
              <div class="row-box" role="note" data-id="${item._id}" data-topic-id="${item.topic_id}">
                <div class="row row-header">
                  <div class="col-xs-8 row-title"><strong>${item.name}</strong></div>
                  <div class="col-xs-4 row">
                    <div class="col-xs-4 p0 tac row-count"><p><strong>${item.like_count||0}</strong></p><p><sub>收藏</sub></p></div>
                    <div class="col-xs-4 p0 tac row-count"><p><strong>${item.ans_count||0}</strong></p><p><sub>回复</sub></p></div>
                    <div class="col-xs-4 p0 tac row-count"><p><strong role="author" data-id="${item.author_id}">${item.author_name}</strong></p><p><sub>作者</sub></p></div>               
                  </div>
                </div><!-- row-header end  -->
                <div class="row row-body">
                  <div class="col-xs-12 tac refresh p0 ">
                    <i class="glyphicon glyphicon-refresh"></i>
                  </div>
                </div>
                <!-- row-body end -->
                <div class="row row-footer">
                  <div class="col-xs-12 tac" role="toggle">
                    <i class="glyphicon glyphicon-chevron-down"></i>
                  </div>
                </div>
              </div>
              `
            }
          });
          $('#tab-pane-other').html(html)
          
        }
      })
      
    }
  })
})
// 最新帖子
$(window).on('inited',function(){
  var pageCount=0;
  var pageNum=0;
  var gettingData=false;
  var notesHtml='';
  function noteRender(pageNumber) {
    // var reqData={
    //   pageSize:15,      
    //   pageNum:pageNumber||0,
    //   query:{statu:{"$ne":"baned"},tar_id:'0000'}
    // }
    var banedTopic,banedUser,noteData={};
    $.when(
      $.post('/topic/query',{
        query:{$or:[{statu:'baned'}]}
      },function (resData) {;
        banedTopic=resData
      }),
      $.post('/user/query',{
        query:{$or:[{statu:'baned'}]}
      },function (resData) {;
        banedUser=resData
      }),
      $.post('/note/query',{
        pageSize:15,      
        pageNum:pageNumber||0,
        query:{statu:{"$ne":"baned"},tar_id:'0000'}
      },function(resData) {
        noteData=resData
      })
    ).done(function () {
      if(banedTopic.code==200&&noteData.code==200&&banedUser.code==200){
        var banedTopicIdArr=[];
        var banedUserIdArr=[];
        banedTopic.data.forEach(item => {
          banedTopicIdArr.push(item._id)
        });
        banedUser.data.forEach(item => {
          banedUserIdArr.push(item._id)
        });
        gettingData=false;
        pageCount=noteData.pageCount;
        pageNum=noteData.pageNum;
        noteData.data.forEach(item => {
          console.log(banedTopic)
          if(banedTopicIdArr.indexOf(item.topic_id)==-1&&banedUserIdArr.indexOf(item.author_id)==-1){
            notesHtml+=`
            <div class="row-box" role="note" data-id="${item._id}" data-topic-id="${item.topic_id}">
              <div class="row row-header">
                <div class="col-xs-8 row-title"><strong>${item.name}</strong></div>
                <div class="col-xs-4 row">
                  <div class="col-xs-4 p0 tac row-count"><p><strong>${item.like_count||0}</strong></p><p><sub>收藏</sub></p></div>
                  <div class="col-xs-4 p0 tac row-count"><p><strong>${item.ans_count||0}</strong></p><p><sub>回复</sub></p></div>
                  <div class="col-xs-4 p0 tac row-count"><p><strong role="author" data-id="${item.author_id}">${item.author_name}</strong></p><p><sub>作者</sub></p></div>               
                </div>
              </div><!-- row-header end  -->
              <div class="row row-body">
                <div class="col-xs-12 tac refresh p0 ">
                  <i class="glyphicon glyphicon-refresh"></i>
                </div>
              </div>
              <!-- row-body end -->
              <div class="row row-footer">
                <div class="col-xs-12 tac" role="toggle">
                  <i class="glyphicon glyphicon-chevron-down"></i>
                </div>
              </div>
            </div>
            `
          }
        });
        $("#home").html(notesHtml);
      }
    })
  
  }noteRender();
  $('#home-toggle').on('click',function() {
    pageCount=0;
    pageNum=0;
    gettingData=false;
    notesHtml='';
    noteRender();
  })
  $('#row-box-container').on('scroll',function(e){
    // console.log(e)
    var cis= $(this).find('#home [role=note]')
    var height=0;
    for (let index = 0; index < cis.length; index++) {
      const item = cis[index];
      height+=$(item).height()
    }
    // console.log(height - $(this).scrollTop())
    // console.log($('#home').height())
    if( height - $(this).scrollTop() < $('#home').height()){
      if(pageNum<pageCount&&!gettingData){
        gettingData=true;
        noteRender(pageNum+1>pageCount?pageCount:pageNum+1)
        // console.log(pageNum,pageCount)
      }
    }
  })
})
// 我的帖子
$(window).on('inited',function(){
  var pageCount=0;
  var pageNum=0;
  var gettingData=false;
  var notesHtml='';
  function noteRender(pageNumber) {
    var reqData={
      pageSize:15,      
      pageNum:pageNumber||0,
      query:{
        author_name:__.data('user').name,
        statu:{"$ne":"baned"},
        tar_id:'0000'
      }
    }
    $.post('/note/query',reqData,function(resData) {
      // console.log(resData)
      if(resData.code==200){
        gettingData=false;
        pageCount=resData.pageCount;
        pageNum=resData.pageNum;
        if(resData.data.length<=0){
          return;
        }
        resData.data.forEach(item => {
          notesHtml+=`
          <div class="row-box" role="note" data-id="${item._id}" data-topic-id="${item.topic_id}">
            <div class="row row-header">
              <div class="col-xs-8 row-title"><strong>${item.name}</strong></div>
              <div class="col-xs-4 row">
                <div class="col-xs-4 p0 tac row-count"><p><strong>${item.like_count||0}</strong></p><p><sub>收藏</sub></p></div>
                <div class="col-xs-4 p0 tac row-count"><p><strong>${item.ans_count||0}</strong></p><p><sub>回复</sub></p></div>
                <div class="col-xs-4 p0 tac row-count" role="del_note" data-id="${item._id}"><p><strong>删除</strong></p><p><sub></sub></p></div>               
              </div>
            </div><!-- row-header end  -->
            <div class="row row-body">
              <div class="col-xs-12 tac refresh p0 ">
                <i class="glyphicon glyphicon-refresh"></i>
              </div>
            </div>
            <!-- row-body end -->
            <div class="row row-footer">
              <div class="col-xs-12 tac" role="toggle">
                <i class="glyphicon glyphicon-chevron-down"></i>
              </div>
            </div>
          </div>
          `
        });
        $("#my-note").html(notesHtml);
      }
    })
  }noteRender();
  $('#my-note-toggle').on('click',function() {
    pageCount=0;
    pageNum=0;
    gettingData=false;
    notesHtml='';
    noteRender();
  })
  $('#row-box-container').on('scroll',function(e){
    // console.log(e)
    var cis= $(this).find('#home [role=note]')
    var height=0;
    for (let index = 0; index < cis.length; index++) {
      const item = cis[index];
      height+=$(item).height()
    }
    // console.log(height - $(this).scrollTop())
    // console.log($('#home').height())
    if( height - $(this).scrollTop() < $('#home').height()){
      if(pageNum<pageCount&&!gettingData){
        gettingData=true;
        noteRender(pageNum+1>pageCount?pageCount:pageNum+1)
        // console.log(pageNum,pageCount)
      }
    }
  })
})
// 收藏的帖子
$(window).on('inited',function(){
  var pageCount=0;
  var pageNum=0;
  var gettingData=false;
  var notesHtml='';
  function noteRender(pageNumber) {
    getUser(function() {
      var like_list=__.data('user').like_list||[];
      var like_arr=[]
      like_list.forEach(item => {
        if(item!=""){
          var obj={
            _id:item,
            statu:{"$ne":"baned"},   
            tar_id:'0000'       
          }
          like_arr.push(obj)
        }
      });
      if(like_arr.length<=0){
        $('#like-note').html(`
        <div class="jumbotron">
          <h1>很抱歉</h1>
          <p>你好像没有收藏任何主题</p>
        </div>
        `)
        return;
      }
      var reqData={
        pageSize:15,      
        pageNum:pageNumber||0,
        query:{
          $or:like_arr,
        }
      }
      $.post('/note/query',reqData,function(resData) {
        // console.log(resData)
        if(resData.code==200){
          gettingData=false;
          pageCount=resData.pageCount;
          pageNum=resData.pageNum;
          resData.data.forEach(item => {
            notesHtml+=`
            <div class="row-box" role="note" data-id="${item._id}" data-topic-id="${item.topic_id}">
              <div class="row row-header">
                <div class="col-xs-8 row-title"><strong>${item.name}</strong></div>
                <div class="col-xs-4 row">
                  <div class="col-xs-4 p0 tac row-count"><p><strong>${item.like_count||0}</strong></p><p><sub>收藏</sub></p></div>
                  <div class="col-xs-4 p0 tac row-count"><p><strong>${item.ans_count||0}</strong></p><p><sub>回复</sub></p></div>
                  <div class="col-xs-4 p0 tac row-count"><p><strong role="author" data-id="${item.author_id}">${item.author_name}</strong></p><p><sub>作者</sub></p></div>               
                </div>
              </div><!-- row-header end  -->
              <div class="row row-body">
                <div class="col-xs-12 tac refresh p0 ">
                  <i class="glyphicon glyphicon-refresh"></i>
                </div>
              </div>
              <!-- row-body end -->
              <div class="row row-footer">
                <div class="col-xs-12 tac" role="toggle">
                  <i class="glyphicon glyphicon-chevron-down"></i>
                </div>
              </div>
            </div>
            `
          });
          $("#like-note").html(notesHtml);
        }
      })
    })
    
  }
  $('#like-note-toggle').on('click',function() {
    pageCount=0;
    pageNum=0;
    gettingData=false;
    notesHtml='';
    noteRender();
  })
  $('#row-box-container').on('scroll',function(e){
    // console.log(e)
    var cis= $(this).find('#home [role=note]')
    var height=0;
    for (let index = 0; index < cis.length; index++) {
      const item = cis[index];
      height+=$(item).height()
    }
    // console.log(height - $(this).scrollTop())
    // console.log($('#home').height())
    if( height - $(this).scrollTop() < $('#home').height()){
      if(pageNum<pageCount&&!gettingData){
        gettingData=true;
        noteRender(pageNum+1>pageCount?pageCount:pageNum+1)
        // console.log(pageNum,pageCount)
      }
    }
  })
})
// 收藏的话题
$(window).on('inited',function(){
  var pageCount=0;
  var pageNum=0;
  var gettingData=false;
  var notesHtml='';
  function noteRender(pageNumber) {
    getUser(function() {
      var topic_like_list=__.data('user').topic_like_list||[];
      var like_arr=[]
      topic_like_list.forEach(item => {
        if(item!=""){
          var obj={
            _id:item,
            statu:{"$ne":"baned"},          
          }
          like_arr.push(obj)
        }
      });
      if(like_arr.length<=0){
        $('#like-topic').html(`
        <div class="jumbotron">
          <h1>很抱歉</h1>
          <p>你好像没有收藏任何话题</p>
        </div>
        `)
        return;
      }
      // console.log(like_arr)
      var reqData={
        pageSize:15,      
        pageNum:pageNumber||0,
        query:{
          $or:like_arr,
        }
      }
      $.post('/topic/query',reqData,function(resData) {
        // console.log(resData)
        if(resData.code==200){
          gettingData=false;
          pageCount=resData.pageCount;
          pageNum=resData.pageNum;
          resData.data.forEach(item => {
            notesHtml+=`
            <div class="row-box" role="topic" data-id="${item._id}" data-admin="${item.admin_id||''}">
              <div class="row row-header">
                <div class="col-xs-8 row-title"><strong>${item.name}</strong></div>
                <div class="col-xs-4 row">
                  <div class="col-xs-4 p0 tac row-count"><p>${!!!item.admin_name||item.admin_name==""?`<strong data-id="${item._id}" data-name="${item.name}" role="be_admin">成为版主！</strong>`:`<strong>${item.admin_name||''}</strong>`}</p><p><sub>管理员</sub></p></div>              
                  <div class="col-xs-4 p0 tac row-count"><p><strong>${item.like_count||0}</strong></p><p><sub>关注</sub></p></div>
                  <div class="col-xs-4 p0 tac row-count"><p><strong>${item.p0000_count||0}</strong></p><p><sub>主题</sub></p></div>                  
                </div>
              </div><!-- row-header end  -->
              <div class="row row-body">
                <div class="col-xs-12 tac refresh p0 ">
                  <i class="glyphicon glyphicon-refresh"></i>
                </div>
              </div>
              <button class="btn btn-info add-btn" role="likeTip" data-name="${item.name}" data-id="${item._id}"><i class="glyphicon glyphicon-star"></i></button>            
              <button class="btn btn-info add-btn" role="addTip" data-name="${item.name}" data-id="${item._id}"><i class="glyphicon glyphicon-send"></i></button>            
              <!-- row-body end -->
              <div class="row row-footer">
                <div class="col-xs-12 tac" role="toggle">
                  <i class="glyphicon glyphicon-chevron-down"></i>
                </div>
              </div>
            </div>
            `
          });
          $("#like-topic").html(notesHtml);
        }
      })
    })
    
  }
  $('#like-topic-toggle').on('click',function() {
    pageCount=0;
    pageNum=0;
    gettingData=false;
    notesHtml='';
    noteRender();
  })
  $('#row-box-container').on('scroll',function(e){
    // console.log(e)
    var cis= $(this).find('#home [role=note]')
    var height=0;
    for (let index = 0; index < cis.length; index++) {
      const item = cis[index];
      height+=$(item).height()
    }
    // console.log(height - $(this).scrollTop())
    // console.log($('#home').height())
    if( height - $(this).scrollTop() < $('#home').height()){
      if(pageNum<pageCount&&!gettingData){
        gettingData=true;
        noteRender(pageNum+1>pageCount?pageCount:pageNum+1)
        // console.log(pageNum,pageCount)
      }
    }
  })
})
// 管理的话题
$(window).on('inited',function(){
  var pageCount=0;
  var pageNum=0;
  var gettingData=false;
  var notesHtml='';
  function noteRender(pageNumber) {
    getUser(function() {
      var topic_admin_list=__.data('user').topic_admin_list||[];
     
      var like_arr=[]
      topic_admin_list.forEach(item => {
        if(item!=""){
          var obj={
            _id:item,
            statu:{"$ne":"baned"},          
          }
          like_arr.push(obj)
        }
      });
      if(like_arr.length<=0){
        return;
      }
      // console.log(like_arr)
      var reqData={
        pageSize:15,      
        pageNum:pageNumber||0,
        query:{
          $or:like_arr,
        }
      }
      $.post('/topic/query',reqData,function(resData) {
        // console.log(resData)
        if(resData.code==200){
          gettingData=false;
          pageCount=resData.pageCount;
          pageNum=resData.pageNum;
          resData.data.forEach(item => {
            notesHtml+=`
            <div class="row-box" role="topic" data-id="${item._id}" data-admin="${item.admin_id||''}">
              <div class="row row-header">
                <div class="col-xs-8 row-title"><strong>${item.name}</strong></div>
                <div class="col-xs-4 row">
                  <div class="col-xs-4 p0 tac row-count"><p>${!!!item.admin_name||item.admin_name==""?`<strong data-id="${item._id}" data-name="${item.name}" role="be_admin">成为版主！</strong>`:`<strong>${item.admin_name||''}</strong>`}</p><p><sub>管理员</sub></p></div>              
                  <div class="col-xs-4 p0 tac row-count"><p><strong>${item.like_count||0}</strong></p><p><sub>关注</sub></p></div>
                  <div class="col-xs-4 p0 tac row-count"><p><strong>${item.p0000_count||0}</strong></p><p><sub>主题</sub></p></div>                  
                </div>
              </div><!-- row-header end  -->
              <div class="row row-body">
                <div class="col-xs-12 tac refresh p0 ">
                  <i class="glyphicon glyphicon-refresh"></i>
                </div>
              </div>
              <button class="btn btn-info add-btn" role="likeTip" data-name="${item.name}" data-id="${item._id}"><i class="glyphicon glyphicon-star"></i></button>            
              <button class="btn btn-info add-btn" role="addTip" data-name="${item.name}" data-id="${item._id}"><i class="glyphicon glyphicon-send"></i></button>            
              <!-- row-body end -->
              <div class="row row-footer">
                <div class="col-xs-12 tac" role="toggle">
                  <i class="glyphicon glyphicon-chevron-down"></i>
                </div>
              </div>
            </div>
            `
          });
          $("#admin-topic").html(notesHtml);
        }
      })
    })
    
  }
  $('#admin-topic-toggle').on('click',function() {
    pageCount=0;
    pageNum=0;
    gettingData=false;
    notesHtml='';
    noteRender();
  })
  $('#row-box-container').on('scroll',function(e){
    // console.log(e)
    var cis= $(this).find('#home [role=note]')
    var height=0;
    for (let index = 0; index < cis.length; index++) {
      const item = cis[index];
      height+=$(item).height()
    }
    // console.log(height - $(this).scrollTop())
    // console.log($('#home').height())
    if( height - $(this).scrollTop() < $('#home').height()){
      if(pageNum<pageCount&&!gettingData){
        gettingData=true;
        noteRender(pageNum+1>pageCount?pageCount:pageNum+1)
        // console.log(pageNum,pageCount)
      }
    }
  })
})
// 用户设置
$(window).on('inited',function(){
  function setUserInit() {
    getUser(function() {
      $('#my-header').attr('src',__.data('user').head_img)      
      $('#edit-email').off('submit').on('submit',function(e) {
        e.preventDefault();
        var formData=$(e.target).serializeObject();
        if(formData.oEmail==__.data('user').email){
          $.post('/user/updata',{
            query:{_id:__.data('user')._id},
            data:{email:formData.email}
          },function(resData) {
              if(resData.code==200){
                alert('修改成功')
                getUser()
              }  
            })
        }else{
          alert('旧邮箱不正确')
        }
      })
      $('#edit-pwd').off('submit').on('submit',function(e) {
        e.preventDefault();
        var formData=$(e.target).serializeObject();
        if(formData.oPwd==__.data('user').pwd){
          if(formData.cPwd==formData.pwd){
            $.post('/user/updata',{
              query:{_id:__.data('user')._id},
              data:{pwd:formData.pwd}
            },function(resData) {
              if(resData.code==200){
                alert('修改成功')
                getUser()
              }  
            })
          }else{
            alert('新密码和确认密码不一致')
          }
        }else{
          alert('旧邮箱不正确')
        }
      })
      $('#edit-header').off('submit').on('submit',function (e) {
        e.preventDefault()
        var formData=$(e.target).serializeObject();
        $.post('/user/updata',{
          query:{_id:__.data('user')._id},
          data:{head_img:formData.head_img}
        },function(resData) {
          if(resData.code==200){
            alert('修改成功')
            getUser()
          }  
        })
      })
      $('#head_img_submit').off('click').on('click',function () {
        $('#edit-header').trigger('submit')
      })
      $('#up_img').on('submit',function(e) {
        e.preventDefault();
        // 获取表单提交的数据
        var formData = new FormData(this);
        $.ajax({
          url:'/up_img',
          method:"post",
          data:formData,
          contentType:false,
          processData:false,
          success:function(resData){
            // console.log(resData);
            if(resData.code==200);
            $('#my-header').attr('src',resData.url)
            $('#nHead').val(resData.url)
          }
        })
      })
      $('#uploadImg').on('change',function(e) {
        $('#up_img').trigger('submit');
      })
    })
  }setUserInit()
  $('#my-settings-toggle').on('click',function () {
    setUserInit()
  })

})
// 后台管理
$(window).on('inited',function(){
  getUser(function () {
    if(__.data('user').statu=="admin"||__.data('user').statu=="super-admin"){
      function mNoteRender() {
        var key='note'
        var pageCount=0;
        var pageNum=0;
        var gettingData=false;
        var html='';
        var query={tar_id:"0000"}
        function render(pageNumber) {
          var topicData,noteData={}
          $.when(
            $.post('/'+key+'/query',{
              pageSize:15,      
              pageNum:pageNumber||0,
              query:query
            },function(resData){
              noteData=resData;
            })
          ).done(function(){
            if(noteData.code==200){
              gettingData=false;
              pageCount=noteData.pageCount;
              pageNum=noteData.pageNum;
              noteData.data.forEach(item => {
                if(!!!item.is_del||item.is_del!=true){
                  html+=`
                  <tr>
                    <td>${item.name||'回复 '+item.tar_author_name}</td>
                    <td>${item.author_name}</td>
                    <td>${item.topic_name}</td>
                    <td>
                    ${item.statu=="baned"?`
                      <a class="btn btn-info" role="unban" data-id="${item._id}">解禁</a>      
                    `:`
                      <a class="btn btn-danger" role="ban" data-id="${item._id}">封禁</a>      
                    `}
                    </td>
                  </tr>
                  `
                }
              });
              $('#manager-'+key+'-tbody').html(html)
            }
          })
      
        }render()
        $('#manager-'+key+'-tbody').each(function(){
          var mnH=$(this).parents('#manager-'+key).height();
          var psH=0;
          $(this).parent().siblings().each(function(){
            psH+=$(this).height()
          })
          $(this).siblings().each(function(){
            psH+=$(this).height()
          })
          thisH=mnH-psH-200;
          $(this).height(thisH)
        })
        $('#manager-'+key+'-tbody').on('scroll',function(e){
          var thisH=$(this).height()
          var cH=0;
          $(this).find('tr').each(function(){
            cH+=$(this).height()
          })
          if(cH-$(this).scrollTop()<=thisH){
            if(pageNum<pageCount&&!gettingData){
              gettingData=true;
              render(pageNum+1>pageCount?pageCount:pageNum+1)
            }
          }
      
        })
        $('#manager-'+key+'-tbody').on('click','[role=ban]',function(e){
          var _id=$(this).attr('data-id');
          var _this=this;
          console.log(this)
          $.post('/note/updata',{query:{_id},data:{statu:'baned'}},function(resData){
            if(resData.code==200){
              alert('封禁成功')
              $(_this).attr('role','unban').removeClass('btn-danger').addClass('btn-info').text('解封')
            }
          })
        })
        $('#manager-'+key+'-tbody').on('click','[role=unban]',function(e){
          var _id=$(this).attr('data-id');
          var _this=this;
          $.post('/note/updata',{query:{_id},data:{statu:'normal'}},function(resData){
            if(resData.code==200){
              alert('解封成功')
              $(_this).attr('role','ban').removeClass('btn-info').addClass('btn-danger').text('封禁')
            }
          })
        })
        $('#manager-'+key+'-form').on('submit',function(e){
          e.preventDefault()
          var formData=$(this).serializeObject();
          if(!formData.name||formData.name.trim()==''){
            query={tar_id:"0000"}
          }else{
            query={
              name:formData.name.trim(),
              $reg:{
                name:true,
              },
              tar_id:"0000"
            }
          }
          html='';
          render()
        })
      }
      function mTopicRender() {
        var key='topic'
        var pageCount=0;
        var pageNum=0;
        var gettingData=false;
        var html='';
        var query={}
        function render(pageNumber) {
          var topicData,noteData={}
          $.when(
            $.post('/'+key+'/query',{
              pageSize:15,      
              pageNum:pageNumber||0,
              query:query
            },function(resData){
              noteData=resData;
            })
          ).done(function(){
            if(noteData.code==200){
              gettingData=false;
              pageCount=noteData.pageCount;
              pageNum=noteData.pageNum;
              noteData.data.forEach(item => {
                console.log(item)
                html+=`
                <tr>
                  <td>${item.name}</td>
                  <td>${item.p0000_count||0}</td>
                  <td>${item.admin_name||'尚无管理员'}</td>
                  <td>
                  ${item.statu=="baned"?`
                    <a class="btn btn-info" role="unban" data-id="${item._id}">解禁</a>      
                  `:`
                    <a class="btn btn-danger" role="ban" data-id="${item._id}">封禁</a>      
                  `}
                  ${!!item.admin_id||item.admin_id.trim()!=''?`
                  <a class="btn btn-info" role="del_admin" data-id="${item._id}" data-admin="${item.admin_id}">撤除管理员</a> 
                  `:""}
                  </td>
                </tr>
                `
              });
              $('#manager-'+key+'-tbody').html(html)
            }
          })
      
        }render()
        $('#manager-'+key+'-tbody').each(function(){
          var mnH=$(this).parents('#manager-'+key).height();
          var psH=0;
          $(this).parent().siblings().each(function(){
            psH+=$(this).height()
          })
          $(this).siblings().each(function(){
            psH+=$(this).height()
          })
          thisH=mnH-psH-200;
          $(this).height(thisH)
        })
        $('#manager-'+key+'-tbody').on('scroll',function(e){
          var thisH=$(this).height()
          var cH=0;
          $(this).find('tr').each(function(){
            cH+=$(this).height()
          })
          if(cH-$(this).scrollTop()<=thisH){
            if(pageNum<pageCount&&!gettingData){
              gettingData=true;
              render(pageNum+1>pageCount?pageCount:pageNum+1)
            }
          }
      
        })
        $('#manager-'+key+'-tbody').on('click','[role=ban]',function(e){
          var _id=$(this).attr('data-id');
          var _this=this;
          console.log(this)
          $.post('/'+key+'/updata',{query:{_id},data:{statu:'baned'}},function(resData){
            if(resData.code==200){
              alert('封禁成功')
              $(_this).attr('role','unban').removeClass('btn-danger').addClass('btn-info').text('解封')
            }
          })
        })
        $('#manager-'+key+'-tbody').on('click','[role=unban]',function(e){
          var _id=$(this).attr('data-id');
          var _this=this;
          $.post('/'+key+'/updata',{query:{_id},data:{statu:'normal'}},function(resData){
            if(resData.code==200){
              alert('解封成功')
              $(_this).attr('role','ban').removeClass('btn-info').addClass('btn-danger').text('封禁')
            }
          })
        })
        $('#manager-'+key+'-tbody').on('click','[role=del_admin]',function(e){
          var topicId=$(this).attr('data-id');
          var adminId=$(this).attr('data-admin');
          var _this=this;
          
          $.post('/user/query',{
            query:{_id:adminId}
          },function(adminData){
            if(adminData.code==200){
              var topic_admin_list=adminData.data[0].topic_admin_list;
              topic_admin_list.splice(topic_admin_list.indexOf(topicId),1);
              if(topic_admin_list.length==0)topic_admin_list.push(null)
              var userData={};
              var topicData={};
              $.when(
                $.get('/user/updata',{query:{_id:adminId},data:{topic_admin_list:topic_admin_list}},function(resData){
                  userData=resData;
                }),
                $.post('/topic/updata',{query:{_id:topicId},data:{admin_id:'',admin_name:''}},function(resData){
                  topicData=resData;
                })
              ).done(function(){
                if(userData.code==200&&topicData.code==200){
                  alert('操作成功')
                  $(_this).remove();
                }
              })
            }
          })
         
        })
        $('#manager-'+key+'-form').on('submit',function(e){
          e.preventDefault()
          var formData=$(this).serializeObject();
          if(!formData.name||formData.name.trim()==''){
            query={}
          }else{
            query={
              name:formData.name.trim(),
              $reg:{
                name:true,
              },
            }
          }
          html='';
          render()
        })
      }
      function mUserRender() {
        var key='user'
        var pageCount=0;
        var pageNum=0;
        var gettingData=false;
        var html='';
        var query={}
        function render(pageNumber) {
          var topicData,noteData={}
          $.when(
            $.post('/'+key+'/query',{
              pageSize:15,      
              pageNum:pageNumber||0,
              query:query
            },function(resData){
              noteData=resData;
            })
          ).done(function(){
            if(noteData.code==200){
              gettingData=false;
              pageCount=noteData.pageCount;
              pageNum=noteData.pageNum;
              noteData.data.forEach(item => {
                if(item.statu=="admin"&&__.data('user').statu!="super-admin"&&__.data('user')._id!=item._id){
                  item.email="你无权查看"
                }
                if(item.statu=="super-admin"&&__.data('user').statu!="super-admin"){
                }else{
                  html+=`
                  <tr>
                    <td>${item.name}</td>
                    <td>${item.email}</td>
                    <td>${item.statu}</td>
                    <td>
                    ${item.statu=="baned"?`
                      <a class="btn btn-info" role="unban" data-id="${item._id}">解禁</a>      
                    `:''}
                    ${item.statu=="normal"?`
                      <a class="btn btn-danger" role="ban" data-id="${item._id}">封禁</a>      
                    `:''}
                    ${item.statu=="admin"&&__.data('user').statu=="super-admin"?`
                      <a class="btn btn-danger" role="ban" data-id="${item._id}">封禁</a>                          
                    `:''}
                    ${item.statu=="admin"&&__.data('user').statu=="admin"?`
                    你无权操作管理员
                    `:''}
                    ${item.statu=="normal"&&__.data('user').statu=="super-admin"?`
                    <a class="btn btn-danger" role="set-admin" data-id="${item._id}">设为管理员</a>      
                    `:''}
                    </td>
                  </tr>
                  `
                }
              });
              $('#manager-'+key+'-tbody').html(html)
            }
          })
      
        }render()
        $('#manager-'+key+'-tbody').each(function(){
          var mnH=$(this).parents('#manager-'+key).height();
          var psH=0;
          $(this).parent().siblings().each(function(){
            psH+=$(this).height()
          })
          $(this).siblings().each(function(){
            psH+=$(this).height()
          })
          thisH=mnH-psH-200;
          $(this).height(thisH)
        })
        $('#manager-'+key+'-tbody').on('scroll',function(e){
          var thisH=$(this).height()
          var cH=0;
          $(this).find('tr').each(function(){
            cH+=$(this).height()
          })
          if(cH-$(this).scrollTop()<=thisH){
            if(pageNum<pageCount&&!gettingData){
              gettingData=true;
              render(pageNum+1>pageCount?pageCount:pageNum+1)
            }
          }
      
        })
        $('#manager-'+key+'-tbody').on('click','[role=ban]',function(e){
          var _id=$(this).attr('data-id');
          var _this=this;
          console.log(this)
          $.post('/'+key+'/updata',{query:{_id},data:{statu:'baned'}},function(resData){
            if(resData.code==200){
              alert('封禁成功')
              $(_this).attr('role','unban').removeClass('btn-danger').addClass('btn-info').text('解封')
            }
          })
        })
        $('#manager-'+key+'-tbody').on('click','[role=unban]',function(e){
          var _id=$(this).attr('data-id');
          var _this=this;
          $.post('/'+key+'/updata',{query:{_id},data:{statu:'normal'}},function(resData){
            if(resData.code==200){
              alert('解封成功')
              $(_this).attr('role','ban').removeClass('btn-info').addClass('btn-danger').text('封禁')
            }
          })
        })
        $('#manager-'+key+'-tbody').on('click','[role="set-admin"]',function(e){
          var _id=$(this).attr('data-id');
          var _this=this;
          $.post('/'+key+'/updata',{query:{_id},data:{statu:'admin'}},function(resData){
            if(resData.code==200){
              alert('设置成功')
              $(_this).remove()
            }
          })
        })
        $('#manager-'+key+'-form').on('submit',function(e){
          e.preventDefault()
          var formData=$(this).serializeObject();
          if(!formData.name||formData.name.trim()==''){
            query={}
          }else{
            query={
              name:formData.name.trim(),
              $reg:{
                name:true,
              },
            }
          }
          html='';
          render()
        })
      }
      function mBadminRender() {
        var key='badmin'
        var pageCount=0;
        var pageNum=0;
        var gettingData=false;
        var html='';
        var query={statu:'no-read'}
        function render(pageNumber) {
          var topicData,noteData={}
          $.when(
            $.post('/'+key+'/query',{
              pageSize:15,      
              pageNum:pageNumber||0,
              query:query
            },function(resData){
              noteData=resData;
            })
          ).done(function(){
            if(noteData.code==200){
              gettingData=false;
              pageCount=noteData.pageCount;
              pageNum=noteData.pageNum;
              noteData.data.forEach(item => {
                html+=`
                  <tr>
                    <td>${item.user_name}</td>
                    <td>${item.t_name}</td>
                    <td>${item.phone}</td>
                    <td>${item.topic_name}</td>
                    <td>
                     <a class="btn btn-info" role="agg" data-id="${item._id}">同意</a>      
                     <a class="btn btn-info" role="rej" data-id="${item._id}">拒绝</a>      
                    </td>
                  </tr>
                  `
              });
              $('#manager-'+key+'-tbody').html(html)
            }
          })
      
        }render()
        $('#manager-'+key+'-tbody').each(function(){
          var mnH=$(this).parents('#manager-'+key).height();
          var psH=0;
          $(this).parent().siblings().each(function(){
            psH+=$(this).height()
          })
          $(this).siblings().each(function(){
            psH+=$(this).height()
          })
          thisH=mnH-psH-200;
          $(this).height(thisH)
        })
        $('#manager-'+key+'-tbody').on('scroll',function(e){
          var thisH=$(this).height()
          var cH=0;
          $(this).find('tr').each(function(){
            cH+=$(this).height()
          })
          if(cH-$(this).scrollTop()<=thisH){
            if(pageNum<pageCount&&!gettingData){
              gettingData=true;
              render(pageNum+1>pageCount?pageCount:pageNum+1)
            }
          }
      
        })
        $('#manager-'+key+'-tbody').on('click','[role=agg]',function(e){
          var _id=$(this).attr('data-id');
          var _this=this;
          getUser(function(){
            $.get('/badmin/query',{query:{_id}},function(resData){
              if(resData.code==200){
                var baData=resData.data[0];
                var tpcData={}
                var userData={}
                var topic_admin_list=__.data('user').topic_admin_list||[];
                var sys_msg=__.data('user').sys_msg||[];
                if(topic_admin_list.indexOf(baData.topic_id)!=-1){
                  alert('该授权已被其他管理员处理')
                  html='';
                  render();
                  return;
                }
                var date=new Date().toLocaleDateString()
                sys_msg.push(
                  date+`申请成为${baData.topic_name}的版主 已同意，授权人${__.data('user').name}`
                )
                topic_admin_list.push(baData.topic_id)
                $.when(
                  $.post('/topic/updata',{
                    query:{_id:baData.topic_id},
                    data:{
                      admin_id:baData.user_id,
                      admin_name:baData.user_name
                    }
                  },function(resData){
                    tpcData=resData;
                  }),
                  $.post('/user/updata',{
                    query:{_id:__.data('user')._id},
                    data:{
                      topic_admin_list,
                      sys_msg,
                    }
                  },function(resData){
                    userData=resData;
                  })
                ).done(function(){
                  if(tpcData.code==200&&userData.code==200){
                    $.post('/'+key+'/updata',{query:{_id},data:{statu:'readed'}},function(resData){
                      if(resData.code==200){
                        alert('处理成功,已授权')
                        $(_this).parents('tr').first().remove()
                      }
                    })
                  }
                })
              }
            })
          })
          
        })
        $('#manager-'+key+'-tbody').on('click','[role=rej]',function(e){
          var _id=$(this).attr('data-id');
          var _this=this;
          $.post('/'+key+'/updata',{query:{_id},data:{statu:'readed'}},function(resData){
            if(resData.code==200){
              alert('处理成功,已拒绝本次申请')
              $(_this).parents('tr').first().remove()
            }
          })
        })

        $('#manager-'+key+'-form').on('submit',function(e){
          e.preventDefault()
          var formData=$(this).serializeObject();
          if(!formData.name||formData.name.trim()==''){
            query={statu:'no-read'}
          }else{
            query={
              name:formData.name.trim(),
              $reg:{
                name:true,
              },
              statu:'no-read'
            }
          }
          html='';
          render()
        })
      }
      $('#setting-toggle').on('click',function(){
        $('#row-box-container').scrollTop(0)
        mNoteRender()
        mTopicRender()
        mUserRender()
        mBadminRender()
      })
    }else{
      $('#setting-toggle').remove();
    }
  })
})
// 所有话题
$(window).on('inited',function(){
  var pageCount=0;
  var pageNum=0;
  var gettingData=false;
  var notesHtml='';
  function topicRender(pageNumber) {
    var reqData={
      pageSize:15,      
      pageNum:pageNumber||0,
      query:{statu:{"$ne":"baned"}}
    }
    $.post('/topic/query',reqData,function(resData) {
      // console.log(resData)
      if(resData.code==200){
        gettingData=false;
        pageCount=resData.pageCount;
        pageNum=resData.pageNum;
        resData.data.forEach(item => {
          notesHtml+=`
          <div class="row-box" role="topic" data-id="${item._id}" data-admin="${item.admin_id||''}">
            <div class="row row-header">
              <div class="col-xs-8 row-title"><strong>${item.name}</strong></div>
              <div class="col-xs-4 row">
                <div class="col-xs-4 p0 tac row-count"><p>${!!!item.admin_name||item.admin_name==""?`<strong data-id="${item._id}" data-name="${item.name}" role="be_admin">成为版主！</strong>`:`<strong>${item.admin_name||''}</strong>`}</p><p><sub>管理员</sub></p></div>
                <div class="col-xs-4 p0 tac row-count"><p><strong>${item.like_count||0}</strong></p><p><sub>关注</sub></p></div>
                <div class="col-xs-4 p0 tac row-count"><p><strong>${item.p0000_count||0}</strong></p><p><sub>主题</sub></p></div>                  
              </div>
            </div><!-- row-header end  -->
            <div class="row row-body">
              <div class="col-xs-12 tac refresh p0 ">
                <i class="glyphicon glyphicon-refresh"></i>
              </div>
            </div>
            <button class="btn btn-info add-btn" role="likeTip" data-name="${item.name}" data-id="${item._id}"><i class="glyphicon glyphicon-star"></i></button>            
            <button class="btn btn-info add-btn" role="addTip" data-name="${item.name}" data-id="${item._id}"><i class="glyphicon glyphicon-send"></i></button>            
            <!-- row-body end -->
            <div class="row row-footer">
              <div class="col-xs-12 tac" role="toggle">
                <i class="glyphicon glyphicon-chevron-down"></i>
              </div>
            </div>
          </div>
          `
        });
        $("#topies").html(notesHtml);
      }
    })
  }topicRender();
  $('#topic-toggle').on('click',function() {
    pageCount=0;
    pageNum=0;
    gettingData=false;
    notesHtml='';
    topicRender();
  })
  $('#row-box-container').on('scroll',function(e){
    // console.log(e)
    var cis= $(this).find('#topies [role=topic]')
    var height=0;
    for (let index = 0; index < cis.length; index++) {
      const item = cis[index];
      height+=$(item).height()
    }
    // console.log(height - $(this).scrollTop())
    // console.log($('#home').height())
    if( height - $(this).scrollTop() < $('#home').height()){
      if(pageNum<pageCount&&!gettingData){
        gettingData=true;
        topicRender(pageNum+1>pageCount?pageCount:pageNum+1)
        // console.log(pageNum,pageCount)
      }
    }
  })
})
// 话题展示
$(window).on('inited',function(){
  $('#row-box-container').on('click','[role="topic"]>.row-header .row-title',function(){
    var _this=$(this).parents('[role="topic"]').first();
    var id=$(_this).attr('data-id');
    var admin_id=$(_this).attr('data-admin');
    var is_admin=__.data('user')._id==admin_id;
    console.log(is_admin)
    var reqData={
      query:{topic_id:id,tar_id:'0000'}
    }
    getUser(function(){
      var topic_id=$(_this).attr('data-id')
      var topic_like_list=__.data('user').topic_like_list;
      if(topic_like_list.indexOf(topic_id)!=-1){
        $(_this).find('[role=likeTip]').first().attr('role','unLikeTip')
      }
    })
    $.post('/note/query',reqData,function(resData){
      // console.log(resData)
      var pageCount=0;
      var pageNum=0;
      var gettingData=false;
      var notesHtml='';    
      var normalCount=0
      resData.data.forEach(item => {
        if(item.statu=='normal')normalCount++;
      });
      var noteHtml=`
      <div class="row-box active" role="note">
        <div class="row row-header">
          <div class="col-xs-8 row-title"><strong>还没帖子，赶紧发一个？</strong></div>
          <div class="col-xs-4 row">
          </div>
        </div><!-- row-header end  -->
        <div class="row row-body"  style="background-color:#FFF;">
          <div class="col-xs-12 refresh">
            <form id="note_form">
              <div class="form-group">
                <input type="text" class="form-control" id="topic_name" name="name" placeholder="标题">
              </div>
              <div id="qe-box"></div>
              <div class="form-group">
                <button type="submit" class="form-control btn btn-default">发送</button>
              </div>              
            </form>
          </div>
        </div>
        <!-- row-body end -->
        <div class="row row-footer">
          <div class="col-xs-12 tac" role="toggle">
            <i class="glyphicon glyphicon-chevron-down"></i>
          </div>
        </div>
      </div>
      `;
      if(resData.data.length==0||(normalCount==0&&!is_admin&&resData.data.length!=0)){
        $(_this).find('.row-body').html(noteHtml)
        $(_this).find('.row-body').find('#qe-box').quickEdit({
          font:true,
          fontSize:true,
          style:true,
          list:true,
          align:true,
          link:true,
          media:true,
          rdud:true
        })
        $(_this).find('.row-body').css({
          height:'500px'
        })
        $(_this).find('.row-body').find('#note_form').on('submit',function(e){
          e.preventDefault()
          if(!$(e.target).serializeObject().name||$(e.target).serializeObject().name.trim()==''){
            alert('请输入标题');
            return;
          }
          if(!$(e.target).find('#qe-box .editor').text()||$(e.target).find('#qe-box .editor').text().trim()==''){
            alert('内容不能为空');
            return;
          }
          var reqData={
            userQuery:{
              name:__.data('user').name
            },
            topicQuery:{
              "_id":$(_this).attr('data-id')
            },
            noteQuery:{
              name:$(e.target).serializeObject().name,
              content:$(e.target).find('#qe-box .editor').html()
            }
          }
          console.dir(reqData)
          $.post('/note/add',reqData,function(resData){
            if(resData.code==200){
              alert('发送成功')
              TopicRender()        
            }
          })
        })
      }else{
        TopicRender()
      }
      function TopicRender(pageNumber){
        reqData.pageSize=6;
        reqData.pageNum=pageNumber||0,
        $.post('/note/query',reqData,function(resData){
          // console.log(resData)
          if(resData.code==200){
            gettingData=false;
            pageCount=resData.pageCount;
            pageNum=resData.pageNum;
            resData.data.forEach(item => {
              if(item.statu!='normal'&&!is_admin){
                return
              }
              if(item.is_del){
                return
              }
              notesHtml+=`
              <div class="row-box" role="note" data-id="${item._id}" data-topic-id="${item.topic_id}">
                <div class="row row-header">
                  <div class="col-xs-8 row-title"><strong>${item.name}</strong></div>
                  <div class="col-xs-4 row">
                    ${is_admin?`
                    <div class="col-xs-3 p0 tac row-count"><p><strong>${item.like_count||0}</strong></p><p><sub>收藏</sub></p></div>
                    <div class="col-xs-3 p0 tac row-count"><p><strong>${item.ans_count||0}</strong></p><p><sub>回复</sub></p></div>
                    <div class="col-xs-3 p0 tac row-count"><p><strong role="author" data-id="${item.author_id}">${item.author_name}</strong></p><p><sub>作者</sub></p></div>               
                    ${item.statu=='normal'?`
                    <div class="col-xs-3 p0 tac row-count"><p><strong role="ban" data-id="${item._id}">封禁</strong></p><p><sub></sub></p></div>               
                    `:`
                    <div class="col-xs-3 p0 tac row-count"><p><strong role="unban" data-id="${item._id}">解封</strong></p><p><sub></sub></p></div>               
                    `}
                    `:`
                    <div class="col-xs-4 p0 tac row-count"><p><strong>${item.like_count||0}</strong></p><p><sub>收藏</sub></p></div>
                    <div class="col-xs-4 p0 tac row-count"><p><strong>${item.ans_count||0}</strong></p><p><sub>回复</sub></p></div>
                    <div class="col-xs-4 p0 tac row-count"><p><strong role="author" data-id="${item.author_id}">${item.author_name}</strong></p><p><sub>作者</sub></p></div>               
                    `}
                  </div>
                </div><!-- row-header end  -->
                <div class="row row-body">
                  <div class="col-xs-12 tac refresh p0 ">
                    <i class="glyphicon glyphicon-refresh"></i>
                  </div>
                </div>
                <!-- row-body end -->
                <div class="row row-footer">
                  <div class="col-xs-12 tac" role="toggle">
                    <i class="glyphicon glyphicon-chevron-down"></i>
                  </div>
                </div>
              </div>
              `
            });
            $(_this).find('.row-body').html(notesHtml);
            $(_this).find('.row-body').css({
              'height':'500px',
              // 帖子可视高度
            })
          }
        })
      }
      _this.off('click');
      _this.on('click','[role=addTip]',function(e){
        var _note=this;
        // console.log($(_note))
        // console.log($(_note).attr('data-name'))
        var modal=$.bootModal(`发送到 ${$(_note).attr('data-name')}：`,`
        <form id="tpc_form">
          <div class="form-group">
            <label for="topic_name">话题名称：</label>
            <input type="text" class="form-control" name="name" placeholder="">
          </div>
          <div id="tpc_qe"></div>          
        </form>
        `)
        modal.show();
        modal.find('#tpc_qe').quickEdit({
          font:true,
          fontSize:true,
          style:true,
          list:true,
          align:true,
          link:true,
          media:true,
          rdud:true
        })
        modal.on('submitClick',function(){
          if(!modal.find('#tpc_form [name=name]').val()||modal.find('#tpc_form [name=name]').val().trim()==''){
            alert('标题不能为空');
            return;
          }
          if(!modal.find('#tpc_qe .editor').text()||modal.find('#tpc_qe .editor').text().trim()==''){
            alert('内容不能为空');
            return;
          }
          var reqData={
            userQuery:{
              name:__.data('user').name
            },
            topicQuery:{
              "_id":$(_this).attr('data-id')
            },
            noteQuery:{
              name:modal.find('#tpc_form [name=name]').val(),
              content:modal.find('#tpc_qe .editor').html()
            }
          }
          // console.log(_this)
          var addData={};
          $.when(
            $.post('/note/add',reqData,function(resData){
              addData=resData;
            }),
          ).done(function() {
            if(addData.code==200){
              alert('发送成功')
              modal.hide();
              notesHtml=''
              TopicRender();
            }
          })
        })
      })
      _this.on('click','[role=ban]',function(e){
        var _id=$(this).attr('data-id');
        var _this=$(this).parents('[role="note"]').first();
        var banBtn=this;
        if(confirm('确定要封禁该帖子？')){
          $.post('/note/updata',{query:{_id},data:{statu:'baned'}},function(resData){
            if(resData.code==200){
              alert('封禁成功')
              console.log(banBtn)
              $(banBtn).attr('role','unban').html('解封')
            }
          })
        }
      })
      _this.on('click','[role=unban]',function(e){
        var _id=$(this).attr('data-id');
        var _this=$(this).parents('[role="note"]').first();
        var banBtn=this;        
        if(confirm('确定要解封该帖子？')){
          $.post('/note/updata',{query:{_id},data:{statu:'normal'}},function(resData){
            if(resData.code==200){
              alert('解封成功')
              $(banBtn).attr('role','ban').html('封禁')
            }
          })
        }
      })
      _this.on('click','[role=likeTip]',function(e){
        var tar_id=$(this).attr('data-id')
        topicData={}
        getUser(function(){
          $.post('/topic/query',{query:{_id:tar_id}},function(resData){
            if(resData.code==200){
              topicData=resData.data[0];
              var is_like=false;
              var topic_like_list= __.data('user').topic_like_list||[]
              topic_like_list.forEach(item => {
                if(item==tar_id) is_like=true;
              });
              if(!is_like){
                topic_like_list.push(tar_id)
                let like_count=Number(topicData.like_count)||0
                let userData,topicData2={};
                $.when(
                  $.post('/user/updata',{
                    query:{
                      _id:__.data('user')._id
                    },
                    data:{
                      topic_like_list
                    }
                  },function(resData){
                    userData=resData;
                  }),
                  $.post('/topic/updata',{
                    data:{
                      like_count:like_count+1
                    },
                    query:{
                      _id:tar_id
                    },
                  },function(resData){
                    topicData2=resData;
                  })
                ).done(function(){
                  if(topicData2.code==200&&userData.code==200){
                    getUser(function(){
                      alert('收藏成功')
                      $(_this).find('[role=likeTip]').first().attr('role','unLikeTip')
                    })
                  }
                })
              }
            }
          })
        })
      })
      _this.on('click','[role=unLikeTip]',function(e){
        var tar_id=$(this).attr('data-id')
        topicData={}
        getUser(function(){
          $.post('/topic/query',{query:{_id:tar_id}},function(resData){
            if(resData.code==200){
              topicData=resData.data[0];
              var is_like=false;
              var topic_like_list= __.data('user').topic_like_list||[]
              topic_like_list.splice(topic_like_list.indexOf(tar_id),1)
              if(topic_like_list.length==0)topic_like_list.push(null)
              let like_count=Number(topicData.like_count)||0
              let userData,topicData2={};
              $.when(
                $.post('/user/updata',{
                  query:{
                    _id:__.data('user')._id
                  },
                  data:{
                    topic_like_list
                  }
                },function(resData){
                  userData=resData;
                }),
                $.post('/topic/updata',{
                  data:{
                    like_count:like_count-1
                  },
                  query:{
                    _id:tar_id
                  },
                },function(resData){
                  topicData2=resData;
                })
              ).done(function(){
                if(topicData2.code==200&&userData.code==200){
                  getUser(function(){
                    alert('取消收藏成功');
                    $(_this).find('[role=unLikeTip]').first().attr('role','likeTip')
                  })
                }
              })

            }
          })
        })
      })
      _this.find('.row-body').on('scroll',function(e){
        var cis= _this.find('.row-body [role=note]')
        var height=0;
        for (let index = 0; index < cis.length; index++) {
          const item = cis[index];
          height+=$(item).height()
        }
        // console.log(height - $(this).scrollTop())
        if( height - $(this).scrollTop() <500){
          // console.log(pageNum+1>pageCount?pageCount:pageNum+1)
          if(pageNum<pageCount&&!gettingData){
            gettingData=true;
            TopicRender(pageNum+1>pageCount?pageCount:pageNum+1)
            // console.log(pageNum,pageCount)
          }
        }
      })
    })
  })
})
// 帖子展示
$(window).on('inited',function(){
  $('#row-box-container').on('click','[role="note"]>.row-header .row-title',function(e){
    var _this=$(e.target).parents('[role="note"]');
    var id=$(e.target).parents('[role="note"]').attr('data-id');
    // console.log(id)
    var noteData={}
    var ansData={}
    var pageCount=0;
    var pageNum=0;
    var gettingData=false;    
    var noteHtml='';    
    console.log(_this)
    
    function noteRender(pageNumber){
      getUser();      
      $.when(
        $.post('/note/query',{query:{_id:id}},function(resData){noteData=resData;}),
        $.post('/note/query',{
          pageSize:10,
          pageNum:pageNumber||0,
          query:{tar_id:id}
        },function(resData){
          ansData=resData;
          pageCount=resData.pageCount;
          pageNum=resData.pageNum;
          gettingData=false;
        })
      ).done(function(){
        // console.log(noteData,ansData)
        var like_list=__.data('user').like_list;
        var is_like=false;
        if(like_list.indexOf(noteData.data[0]._id)!=-1)is_like=true;
        noteData.data.forEach(item => {
          noteHtml+=`
          <div class="row" role="content-item">
            <div class="col-xs-12"  style="min-height:100px; padding:20px; ">
              ${item.content}
            </div>
            <div class="col-xs-12" style="height:30px; line-height:30px; font-size:14px;">
              <div class="row border-bottom-xs">
                <div class="col-xs-8">
                  <a role="author" data-id="${item.author_id}">${item.author_name}</a> 发表在 ${item.topic_name}
                </div>
                <div class="col-xs-4 tar">
                <a role="ans" data-id="${item.author_id}" data-name="${item.author_name}">回复</a>
                ${is_like?`
                <a role="unlike" data-tar="${item._id}">取消收藏</a>
                `:`
                <a role="like" data-tar="${item._id}">收藏</a>
                `}
                </div>
              </div>
            </div>
          </div>
          `
        });
        ansData.data.forEach(item => {
          noteHtml+=`
          <div class="row" role="content-item">
            <div class="col-xs-12"  style="min-height:100px; padding:20px; ">
              ${item.content}
            </div>
            <div class="col-xs-12" style="height:30px; line-height:30px; font-size:14px;">
              <div class="row border-bottom-xs">
                <div class="col-xs-8">
                  <a role="author" data-id="${item.author_id}">${item.author_name}</a> 回复  <a role="author" data-id="${item.tar_author_id}">${item.tar_author_name}</a> 
                </div>
                <div class="col-xs-4 tar"> <a role="ans" data-id="${item.author_id}" data-name="${item.author_name}">回复</a>  <a role="like" disabled="disabled" data-tar="${item._id}"></a> </div>
              </div>
            </div>
          </div>
          `
        });
        _this.find('.row-body').siblings('[role="ans"]').remove();
        _this.find('.row-body').html(noteHtml).css({
          'height':'500px',
          // 帖子可视高度
        })
        .after(`<button class="btn btn-info add-btn" role="ans"><i class="glyphicon glyphicon-send"></i></button>`)  
      })
    }
    noteRender();
    _this.find('.row-body').on('scroll',function(e){
      var cis= _this.find('.row-body [role="content-item"]')
      var height=0;
      for (let index = 0; index < cis.length; index++) {
        const item = cis[index];
        height+=$(item).height()
      }
      if( height - _this.find('.row-body').scrollTop() <500){
        if(pageNum<pageCount&&!gettingData){
          gettingData=true;
          noteRender(pageNum+1>pageCount?pageCount:pageNum+1)
        }
      }
    })
    _this.off('click')
    _this.on('click','[role=ans]',function(e){
      var _note=this;
      // console.log(_note)
      var modal=$.bootModal(`回复 ${$(_note).attr('data-name')||noteData.data[0].author_name}：`,`<div id="ans_qe"></div>`)
      modal.show();
      modal.find('#ans_qe').quickEdit({
        font:true,
        fontSize:true,
        style:true,
        list:true,
        align:true,
        link:true,
        media:true,
        rdud:true
      })
      modal.on('submitClick',function(){
        console.dir(!!modal.find('#ans_qe .editor').text())
        if(!modal.find('#ans_qe .editor').text()||modal.find('#ans_qe .editor').text().trim()==''){
          alert('内容不能为空');
          return;
        }
        // console.log($(_this).attr('data-topic-id'))
        var reqData={
          userQuery:{
            name:__.data('user').name
          },
          topicQuery:{
            "_id":$(_this).attr('data-topic-id')
          },
          noteQuery:{
            tar_id:noteData.data[0]._id,
            tar_content_id:$(_note).siblings('[role=like]').attr('data-tar')||noteData.data[0]._id,
            tar_author_id:$(_note).attr('data-id')||noteData.data[0].author_id,
            tar_author_name:$(_note).attr('data-name')||noteData.data[0].author_name,
            content:modal.find('#ans_qe .editor').html()
          }
        }
        // console.log(reqData)
        var addData,updataData={};
        // console.log(noteData.data[0])
        var ans_count=Number(noteData.data[0].ans_count)||0  
        $.when(
          $.post('/note/add',reqData,function(resData){
            addData=resData;
          }),
          $.post('/note/updata',{
            query:{
              _id:noteData.data[0]._id,
            },
            data:{
              ans_count:ans_count+1,
            }
          },function (resData) {
            updataData=resData;
          })
        ).done(function() {
          if(addData.code==200&&updataData.code==200){
            alert('发送成功')
            modal.hide();
            noteHtml=''
            noteRender();
          }
        })
      })
    })
    _this.on('click','[role=like]',function(e) {
      var tar_id=$(this).attr('data-tar')
      noteData={}
      getUser(function(){
        $.post('/note/query',{query:{_id:tar_id}},function(resData){
          if(resData.code==200){
            noteData=resData.data[0];
            var is_like=false;
            var like_list= __.data('user').like_list||[]
            like_list.forEach(item => {
              if(item==tar_id) is_like=true;
            });
            if(!is_like){
              like_list.push(tar_id)
              let like_count=Number(noteData.like_count)||0
              let userData,noteData2={};
              $.when(
                $.post('/user/updata',{
                  query:{
                    _id:__.data('user')._id
                  },
                  data:{
                    like_list
                  }
                },function(resData){
                  userData=resData;
                }),
                $.post('/note/updata',{
                  data:{
                    like_count:like_count+1
                  },
                  query:{
                    _id:tar_id
                  },
                },function(resData){
                  noteData2=resData;
                })
              ).done(function(){
                if(noteData2.code==200&&userData.code==200){
                  alert('收藏成功')
                  _this.find('[role=like]').attr('role','unlike').html('取消收藏')
                  
                }
              })
            }else{
              alert('您已经收藏过该帖子')
            }

          }
        })
      })
    })
    _this.on('click','[role=unlike]',function(e) {
      var tar_id=$(this).attr('data-tar');
      noteData={}
      getUser(function(){
        $.post('/note/query',{query:{_id:tar_id}},function(resData){
          if(resData.code==200){
            noteData=resData.data[0];
            var is_like=false;
            var like_list= __.data('user').like_list||[]
            like_list.forEach(item => {
              if(item==tar_id) is_like=true;
            });
            if(is_like){
              like_list.splice(like_list.indexOf(tar_id),1)
              if(like_list.length<=0)like_list.push(null)
              let like_count=Number(noteData.like_count)||0
              let userData,noteData2={};
              $.when(
                $.post('/user/updata',{
                  query:{
                    _id:__.data('user')._id
                  },
                  data:{
                    like_list
                  }
                },function(resData){
                  userData=resData;
                }),
                $.post('/note/updata',{
                  data:{
                    like_count:like_count-1
                  },
                  query:{
                    _id:tar_id
                  },
                },function(resData){
                  noteData2=resData;
                })
              ).done(function(){
                if(noteData2.code==200&&userData.code==200){
                  getUser(function(){
                    alert('取消成功')
                    _this.find('[role=unlike]').attr('role','like').html('收藏')
                  })                  
                }
              })
            }

          }
        })
      })
    })
  })
})
// 删除话题
$(window).on('inited',function(){
  $(document).on('click','[role=note]>.row-header [role=del_note]',function(e){
    e.preventDefault();
    var _id=$(this).attr('data-id')
    var note=$(this).parents('[role=note]').first()
    if(confirm('确定要删除该主题？')){
      $.post('/note/updata',{
        query:{_id},
        data:{
          statu:'baned',
          is_del:true,
        }
      },function(resData){
        if(resData.code==200){
          alert('删除成功')
          note.remove();
        }
      })
    }
  })
})
// 申请版主
$(window).on('inited',function(){
  $(document).on('click','[role=topic]>.row-header [role=be_admin]',function(e){
    var tpc_id=$(this).attr('data-id');
    var tpc_name=$(this).attr('data-name')
    var _this=this;
    var agreeModal=$.bootModal('用户协议',`
    <h3>遵守7条底线</h3>
    <p class="" align="left" style="text-align:left;line-height:150%"><span lang="EN-US" style="line-height:150%;font-family:宋体;color:black">
    1.</span><span style="line-height:150%;font-family:宋体;color:black">法律法规底线：有法可依、有法必依、执法必严、违法必究，任何时候，无论是网上网下，都将始终做到违法必究；<span lang="EN-US"><br>
    2.</span>社会主义制度底线，为我们全面建成小康社会提供了有力地制度保障，我们要积极拥护社会主义及社会主义制度；<span lang="EN-US"><br>
    3.</span>国家利益底线，作为国家公民，时刻维护我们伟大祖国的利益，这也是宪法赋予我们每位公民的光荣义务；<span lang="EN-US"><br>
    4.</span>公民合法权益底线，我们在网络反腐的同时，切记不能以“艳照”等不健康、不正当甚至违法手段对别人进行人身攻击，否则不仅触犯法律，也侵犯了无辜者的合法权益；<span lang="EN-US"><br>
    5.</span>社会公共秩序底线，网络世界必须也要遵循一定的秩序规则，唯有如此大家才能营造一个良好健康的网络环境；<span lang="EN-US"><br>
    6.</span>道德风尚底线，崇尚美德在我国延续几千年的优秀传统，网络空间里也要讲道德，不做有违道德之事；<span lang="EN-US"><br>
    7.</span>信息真实性底线，要求我们在上网时一定要实事求是，而不能以讹传讹、散发谣传，积极宣传政府部门发布的真实信息。
    </span>
    </p>
    <form id="agree-form">
      <input type="checkbox" id="agree" name="agree"> 
      <lable for="agree">我同意</lable>
    </form>
    `,'large')
    agreeModal.find('[role="submit"]').html('提交')
    agreeModal.show()
    agreeModal.on('submitClick',function(){
      if(agreeModal.find('#agree').is(':checked')){
        agreeModal.hide()
        $(_this).trigger('agree');
      }else{
        alert('你必须先同意用户协议！')
      }
    })
    $(_this).on('agree',function(){
      var user_info=__.data('user').t_user_info||{phone:'',t_name:''}
      var BAModal=$.bootModal('信息填写',`
      <form id="ba-form">
        <div class="form-group">
          <label for="">真实姓名：</label>
          <input name="t_name" type="text" class="form-control" id="" placeholder="" value="${user_info.t_name}">
        </div>
        <div class="form-group">
          <label for="">手机号:</label>
          <input name="phone" type="text" class="form-control" id="" placeholder="" value="${user_info.phone}">
        </div>
      </form>
      `).show()
      BAModal.on('submitClick',function(){
        var formData=BAModal.find('#ba-form').serializeObject();
        if(!formData.t_name||formData.t_name.trim()==''){
          alert('姓名不能为空')
          return;
        }
        if(!/^[1][3,4,5,7,8][0-9]{9}$/.test(formData.phone)){
          alert('手机格式不正确')
          return;
        }
        var baData={}
        var upUserData={}
        $.when(
          $.post('/user/updata',{
            query:{_id:__.data('user')._id},
            data:{
              t_user_info:formData,
            }
          },function(resData){
            upUserData=resData;
          }),
          $.post('/badmin/add',{
            query:{
              user_id:__.data('user')._id,
              user_name:__.data('user').name,
              topic_id:tpc_id,
              topic_name:tpc_name,
              phone:formData.phone,
              t_name:formData.t_name,
            }
          },function (resData) {
            baData=resData;              
          })
        ).done(function(){
          if(baData.code==200&&upUserData.code==200){
            BAModal.hide()
            alert('提交成功，请等待审核，工作人员在几个工作日内可能会跟您取得联系，请保持电话畅通')
            getUser();
          }else if(baData.code!=200){
            alert(baData.msg)
          }
        })
      })
    })
  })
})